package com.gxp.common.utils.file;

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;

public class PDFUtils {
    /**
     * PDF添加Comment
     */
    public static void AddComment(String src, String dest, String comment) throws Exception {

        Document document = new Document();
        // 创建PDF写入器
        PdfWriter.getInstance(document, new FileOutputStream(src));
        document.open();

        // 添加注释
        Paragraph paragraph = new Paragraph(comment);
        paragraph.setSpacingBefore(10);
        paragraph.setSpacingAfter(10);
        paragraph.setAlignment(Paragraph.ALIGN_LEFT);
        paragraph.setFont(com.itextpdf.text.FontFactory.getFont(com.itextpdf.text.FontFactory.HELVETICA, 12));
        paragraph.setLeading(1.5f); // 设置行间距
        paragraph.setIndentationLeft(20); // 设置左缩进
        paragraph.setIndentationRight(20); // 设置右缩进
        paragraph.setFirstLineIndent(20); // 设置首行缩进
        paragraph.setExtraParagraphSpace(10); // 设置段落间距
        paragraph.setKeepTogether(true); // 保持段落不分割
//        paragraph.setKeepWithNext(true); // 保持段落与下一个元素在同一页
        paragraph.setMultipliedLeading(1.2f); // 设置多倍行距
//        paragraph.setRunDirection(Paragraph.RUN_DIRECTION_LTR); // 设置文本方向为从左到右
        paragraph.setAlignment(Paragraph.ALIGN_JUSTIFIED); // 设置文本对齐方式为两端对齐
        document.add(paragraph);
        document.close();
    }
}
